package com.tanhua.dubbo.server.api;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.enums.CommentType;
import com.tanhua.dubbo.server.pojo.Comment;
import com.tanhua.dubbo.server.pojo.Publish;
import com.tanhua.dubbo.server.pojo.Video;
import com.tanhua.dubbo.server.vo.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Slf4j
@Service(version = "1.0.0")
public class PublicQuanZiImpl implements PublicQuanZi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private VideoApi videoApi;

    /**
     * 根据id查询动态
     *
     * @param id 动态id
     * @return
     */
    public Publish queryPublishById(String id) {
        return mongoTemplate.findById(id, Publish.class);
    }


    /**
     * 保存Comment
     *
     * @return
     */
    public Boolean saveComment(Long userId, String publishId,
                               CommentType commentType, String content) {

        try {
            Comment comment = new Comment();
            comment.setId(ObjectId.get());
            comment.setUserId(userId);
            comment.setPublishId(new ObjectId(publishId));
            //评论类型
            comment.setCommentType(commentType.getType());
            //内容0
            comment.setContent(content);
            comment.setCreated(System.currentTimeMillis());

            Publish publish = queryPublishById(publishId);
            //TODO 其他评论对象，暂不处理
            if (ObjectUtil.isNotEmpty(publish)) {
                comment.setPublishUserId(publish.getUserId());
            } else {
                //查询评论
                Comment myComment = queryCommentById(publishId);
                if (ObjectUtil.isNotEmpty(myComment)) {
                    comment.setPublishUserId(myComment.getUserId());
                } else {
                    //TODO 其他情况，比如小视频等
                    //查询小视频
                    Video video = videoApi.queryVideoById(publishId);
                    if (ObjectUtil.isNotEmpty(video)) {
                        comment.setPublishUserId(video.getUserId());
                    } else {
                        //TODO 其他情况
                        return false;
                    }
                }
            }


            mongoTemplate.save(comment);
            return true;
        } catch (Exception e) {
            log.debug("保存Comment出错~ userId = " + userId + ", publishId = " + publishId + ", commentType = " + commentType, e);
        }
        return false;
    }


    /**
     * 根据id查询Comment对象
     *
     * @param id
     * @return
     */
    public Comment queryCommentById(String id) {
        return this.mongoTemplate.findById(new ObjectId(id), Comment.class);
    }


    /**
     * 查询对我的点赞,我的喜欢,我的评论消息列表
     *
     * @param userId
     * @param commentType
     * @param page
     * @param pageSize
     * @return
     */
    public PageInfo<Comment> queryCommentListByUser(Long userId, CommentType commentType, Integer page, Integer pageSize) {
        PageInfo<Comment> pageInfo=new PageInfo<>();
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(pageSize);

        PageRequest pageRequest = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.desc("created")));

        Query query = Query.query(Criteria.where("publishUserId").is(userId)
                .and("commentType").is(commentType.getType())).with(pageRequest);
        List<Comment> comments = mongoTemplate.find(query, Comment.class);

        if (CollUtil.isEmpty(comments)){
            return pageInfo;
        }

        pageInfo.setRecords(comments);

        return pageInfo;
    }
}
